package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.LsReport;
import com.example.xpvideo.entity.PageResult;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.entity.Video;
import com.example.xpvideo.entity.req.PhpReportReq;
import com.example.xpvideo.entity.req.PhpVideoReq;
import com.example.xpvideo.entity.req.UpdateReq;
import com.example.xpvideo.entity.res.PhpReportRes;
import com.example.xpvideo.entity.res.PhpVideoRes;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.LsReportMapper;
import com.example.xpvideo.mapper.PhpReportMapper;
import com.example.xpvideo.service.PhpReportService;
import com.example.xpvideo.service.TimeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PhpReportServiceImpl implements PhpReportService {
    @Autowired
    private PhpReportMapper phpReportMapper;
    @Autowired
    private LsReportMapper lsReportMapper;
    @Autowired
    private TimeService timeService;

    @Override
    public ResponseResult<PageResult<List<PhpReportRes>>> list(PhpReportReq phpReportReq) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();
        String phone = phpReportReq.getPhone();
        Integer status = phpReportReq.getStatus();
        String nickname = phpReportReq.getNickname();

        PageHelper.startPage(page, size);
        List<PhpReportRes> list = phpReportMapper.reportList(Wrappers.<LsReport>query()
                .eq(!ObjectUtils.isEmpty(phone), "lsr.phone", phone)
                .eq(!ObjectUtils.isEmpty(status), "lsr.status", status)
                .like(!ObjectUtils.isEmpty(nickname),"nickname", nickname)
                .eq("is_del",1));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<PhpReportRes>> listPageResult = new PageResult<>(count,pages,
                pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }

    @Override
    public ResponseResult<Object> delete(UpdateReq updateReq) throws ErrorException {
        Integer id = updateReq.getId();
        Integer stage = updateReq.getStage();

        LsReport lsReport = new LsReport();
        lsReport.setId(id);
        lsReport.setIsDel(stage);
        lsReport.setUpdateTime(timeService.getTime());

        int res = lsReportMapper.updateLsReport(lsReport);

        if(res > 0){
            return ResponseResult.responseResult(0,"举报管理删除成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20003);
    }

    @Override
    public ResponseResult<Object> consloe(UpdateReq updateReq) throws ErrorException {
        Integer id = updateReq.getId();
        Integer stage = updateReq.getStage();

        LsReport lsReport = new LsReport();
        lsReport.setId(id);
        lsReport.setStatus(stage);
        lsReport.setUpdateTime(timeService.getTime());

        int res = lsReportMapper.updateLsReport(lsReport);

        if(res > 0){
            return ResponseResult.responseResult(0,"举报反馈处理成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20003);
    }
}
